from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
import jieba
import requests
import os
import json

# 设置Chrome驱动路径
CHROME_DRIVER_PATH = "E:\\git_project\\LuckyPython_RPA\\agent_win\\chromedriver_win32\\chromedriver.exe"
# 设置Chromium执行文件路径
CHROMNIUM_EXE = r"E:\\chromium_114\\Win_x64_1140109_chrome-win\\chrome-win\\chrome.exe"

def chinese_keywords(text):
    '''
        获取中文关键词， 便于搜索
    '''
    res = jieba.cut(text, cut_all=False)
    keywords =  " ".join(res)
    print('keywords=',keywords)
    return keywords

def open_browser():
    chrome_options=webdriver.ChromeOptions()
    chrome_options.binary_location=CHROMNIUM_EXE
    service=Service(CHROME_DRIVER_PATH)
    bor = webdriver.Chrome(service=service, options=chrome_options)
    url = "https://www.wiz.cn/xapp"
    bor.get(url)
    bor.maximize_window()
    return bor

def login_wiz(bor):
    with open('selenium_export/config.json') as f:
        user_info = json.load(f)
    username=user_info['username']
    password=user_info['password']
    username_xpath = '//*[@id="root"]/div[1]/div[3]/div/div/div[3]/div/div[1]/input'
    password_xpath = '//*[@id="root"]/div[1]/div[3]/div/div/div[4]/div/div[1]/input'
    username_input = bor.find_element(By.XPATH, username_xpath)
    username_input.send_keys(username)
    sleep(1)
    password_input = bor.find_element(By.XPATH, password_xpath)
    password_input.send_keys(password)
    sleep(1)
    login_btn_xpath = '//*[@id="root"]/div[1]/div[3]/div/div/div[5]/div/button'
    login_btn = bor.find_element(By.XPATH, login_btn_xpath)
    login_btn.click()
    sleep(60)
    return

    
def download_article(bor, uuid, title, file_base_folder):
    keywords = chinese_keywords(title)
    sleep(1)   # 等待页面登录完毕
    search_xpath ='//*[@id="root"]/div[1]/div/div[2]/div/div[1]/div/div[2]/div/div[1]/div/input'
    search_input = bor.find_element(By.XPATH, search_xpath)
    search_input.clear()
    search_input.send_keys(keywords)
    search_input.send_keys(Keys.ENTER)
    sleep(3)
    article_btn_xpath = '//*[@id="root"]/div[1]/div/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div/div/div/li'         
    article_btn = bor.find_element(By.XPATH, article_btn_xpath)
    article_btn.click()
    sleep(3)
    content_xpath = '/html/body/div[1]/div[1]/div/div[2]/div/div[2]/div/div/div[2]/div/div[2]/div/div/div[2]/div[1]/div[1]/div/div/div[1]/div/div[2]/div/div[1]/div[1]'
    article_content = bor.find_element(By.XPATH, content_xpath)
 
    innerHTML = article_content.get_attribute("innerHTML")
    with open(file_base_folder + "\\" +  uuid + '.html', 'w', encoding='utf-8') as f:
        f.write(innerHTML)
    sleep(3)
    return

if __name__ == '__main__':
    # 准备工作
    bor = open_browser()
    login_wiz(bor)
    # 参数准备
    file_base_folder  = 'E:\\wizNoteExport\\'
    title="这才是数学老师应该教的插值方法，统计学中最强大的武器之一"
    uuid = "90f7967a-ea63-4ec6-9463-8d0e3ac4e821"
    # 下载文章
    download_article(bor, uuid, title, file_base_folder)

    bor.quit()